package com.tangyuan.oss.ossMinio.util;

import com.tangyuan.oss.entity.FileInfo;
import io.minio.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import io.minio.messages.Metadata;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: TangYuan
 * @Date: 2024/6/3 13:50
 * @描述: minio文件操作工具
 */
@Component
public class MinioUtil {
    
    @Resource
    public MinioClient minioClient;
    
    /**
     * 列出所有桶
     * @return {@link List<String>}
     * @Date: 2024/6/3 下午2:01
     */
    public List<String> getAllBucket() throws Exception {
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
    }
    
    /**
     * 创建桶
     * @param bucketName 桶名
     * @Date: 2024/6/3 下午1:55
     */
    public void createBucket(String bucketName) throws Exception {
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!exists) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }
    
    /**
     * 删除桶
     * @param bucketName 桶名
     * @Date: 2024/6/3 下午2:15
     */
    public void deleteBucket(String bucketName) throws Exception {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }
    
    
    /**
     * 上传文件
     * @param inputStream 文件流
     * @param bucketName  桶名
     * @param fileName    文件名
     * @Date: 2024/6/3 下午1:59
     */
    public void uploadFile(InputStream inputStream, String bucketName, String fileName) throws Exception {
        minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(inputStream, -1, Integer.MAX_VALUE).build());
    }
    
    
    /**
     * 列出桶中所有文件
     * @return {@link List<FileInfo>}
     * @Date: 2024/6/3 下午2:01
     */
    public List<FileInfo> getAllFile(String bucketName) throws Exception {
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
        List<FileInfo> fileInfoList = new LinkedList<>();
        for (Result<Item> result : results) {
            FileInfo fileInfo = new FileInfo();
            fileInfo.setFileName(result.get().objectName());
            fileInfo.setEtag(result.get().etag());
            fileInfo.setLastModified(result.get().lastModified());
            fileInfo.setOwner(result.get().owner());
            fileInfo.setSize(result.get().size());
            fileInfo.setStorageClass(result.get().storageClass());
            fileInfo.setLatestFlag(result.get().isLatest());
            fileInfo.setVersionId(result.get().versionId());
            fileInfo.setUserMetadata((Metadata) result.get().userMetadata());
            fileInfo.setUserTags(result.get().userTags());
            fileInfo.setDirFlag(result.get().isDir());
            fileInfo.setEncodingType(result.get().etag());
            fileInfoList.add(fileInfo);
        }
        return fileInfoList;
    }
    
    /**
     * 下载文件
     * @param bucketName 桶名
     * @param fileName   文件名
     * @return {@link InputStream}
     * @Date: 2024/6/3 下午2:15
     */
    public InputStream downLoad(String bucketName, String fileName) throws Exception {
        return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
    }
    
    /**
     * 删除文件
     * @param bucketName 桶名
     * @param fileName   文件名
     * @Date: 2024/6/3 下午2:15
     */
    public void deleteFile(String bucketName, String fileName) throws Exception {
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(fileName).build());
    }
}
